package ogs.game.protocol;


import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class MsgEncoder extends ProtocolEncoderAdapter {
	public static final int HEADER_LENGTH = 8;

	@Override
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
			throws Exception {
		
		GameMsg msg = (GameMsg) message;
		byte[] bytes = msg.data;
		
		int capacity = bytes.length + HEADER_LENGTH;
		
		IoBuffer buffer = IoBuffer.allocate(capacity, false);
		buffer.putInt(msg.protocolCode);
		buffer.putInt(bytes.length);
		buffer.put(bytes);
		
		out.write(buffer);
	}

}
